<?php
namespace Home\Controller;

use Home\Model\Admin;
use Think\Controller;

/**
 * 场次管理
 * @author  qinshan
 * @since   1.0
 */
class ScreenController extends BaseController
{
    public function index()
    {
        //做模糊查询传值
        $showtime=$_GET['showtime'];
        if(!empty($showtime))
        {
            $this->assign('showtime',$showtime);
        }
        $model=D('Screening');
        $count = $model->where("showtime like'%$showtime%'")->count();
        $page = new \Think\Page($count,6);
        $page->setConfig('header','共页数');
        $page->setConfig('prev','上一页');
        $page->setConfig('next','下一页');
        $show = $page->show();
        //分页查询电影数据
        $limit = $page->firstRow . ',' . $page->listRows;
        $list=$model->getAll($showtime,$limit);
        foreach($list as $k=>$v)
        {
            $list[$k]['stime']=date('Y-m-d H:i',$v['stime']);
        }
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->assign('active','screen');
        $this->display();
    }
//添加页面
    public function add()
    {

        if(!empty($_POST))
        {
            //var_dump($_POST);exit;
            if(empty($_POST['price']))
            {
                session('message','单价不能为空');
            }elseif(!preg_match('/^[0-9]+(.[0-9]{1,2})?$/', $_POST['price']))
                {
                    session('message','单价数据类型不正确');
                }else{
                    $data['user_id'] = session('home.id');
                    $data['movie_id'] = I("post.movie_id");
                    $data['room_id'] = I("post.room_id");
                    $data['showtime'] = strtotime(I("post.showtime"));
                    $data['price'] = I("post.price");
                    $data['created_at'] = time();
                    //var_dump($data);exit;
                    $res = M('screening')->add($data);
                    if ($res) {
                        //添加成功
                        session('message','添加成功');
                        $this->redirect('index');
                        return false;
                    } else {
                        session('message','添加失败');
                    }
                }
            }
        $list = M('movie')->order('created_at')->select();
        $room = M('room')->where(['user_id'=>session('home.id')])->select();
        $this->assign('list',$list);
        $this->assign('room',$room);
        $this->display();
    }

    public function update()
    {
        $where['id']=$_GET['id'];
        $model = M('screening')->where($where)->find();
       if (empty($_GET['id'])) {
            throw new \Exception("id不存在");
        }
        $this->assign('model', $model);
        $this->assign('id', $_GET['id']);
        $list = M('movie')->order('created_at')->select();
        $room = M('room')->where(['user_id'=>session('home.id')])->select();
        $this->assign('list',$list);
        $this->assign('room',$room);
        $this->display();

    }
   public function updateA()
    {
        $where['id']=$_GET['id'];
        if(!preg_match('/^[0-9]+(.[0-9]{1,2})?$/', $_POST['price']))
        {
            session('message','单价数据类型不正确');
            $this->redirect('update',['id'=>$_GET['id']]);
        }else {
            $data['movie_id'] = I("post.movie_id");
            $data['room_id'] = I("post.room_id");
            $data['showtime'] = strtotime(I("post.showtime"));
            $data['price'] = I("post.price");
            $data['updated_at'] = time();
            //var_dump($data);exit;
            $res = M('screening')->where($where)->save($data);
            //var_dump($res);exit;
            if ($res) {
                //添加成功
                session('message','修改成功');
                $this->redirect('index');
                return false;
            } else {
                session('message','修改失败');
            }
        }
    }

}